import scrapy

from scrapys.wudiSpyder.wudiSpyder.items import JingcaiTtgGameItem


class Wan500JingcaiBqcSpider(scrapy.Spider):
    name = "Wan500JingcaiBqcSpider"
    allowed_domains = ["500.com"]
    start_urls = [
        "https://trade.500.com/jczq/?playid=272&g=2",  # 500wan 足球半全场竞猜彩选购页面
    ]

    def parse(self, response):
        tables = response.xpath('//*[@id="relativeContainer"]/div/div[3]/table')
        for table in tables:
            trs = table.xpath("tbody/tr")
            for tr in trs:
                fid = tr.xpath("@data-fixtureid").get("data")  # 500wan比赛id
                expect = tr.xpath("@data-processdate").get("data")  # 竞猜比赛期数
                stop_time = tr.xpath("@data-buyendtime").get("data")  # 停止购买时间
                game_no_num = tr.xpath("@data-processname").get("data")  # 竞猜比赛场次
                game_no = tr.xpath("@data-matchnum").get("data")  # 竞猜比赛场次名称

                ss = tr.xpath("td[6]/div[2]/p[@data-value='3-3']/@data-sp").get("data")
                sp = tr.xpath("td[6]/div[2]/p[@data-value='3-1']/@data-sp").get("data")
                sf = tr.xpath("td[6]/div[2]/p[@data-value='3-0']/@data-sp").get("data")
                ps = tr.xpath("td[6]/div[2]/p[@data-value='1-3']/@data-sp").get("data")
                pp = tr.xpath("td[6]/div[2]/p[@data-value='1-1']/@data-sp").get("data")
                pf = tr.xpath("td[6]/div[2]/p[@data-value='1-0']/@data-sp").get("data")
                fs = tr.xpath("td[6]/div[2]/p[@data-value='0-3']/@data-sp").get("data")
                fp = tr.xpath("td[6]/div[2]/p[@data-value='0-1']/@data-sp").get("data")
                ff = tr.xpath("td[6]/div[2]/p[@data-value='0-0']/@data-sp").get("data")
                bonus = "{},{},{},{},{},{},{},{},{}".format(ss, sp, sf, ps, pp, pf, fs, fp,ff)
                # 提取游戏信息
                game_item = JingcaiTtgGameItem()
                game_item['fid'] = fid
                game_item['expect'] = expect
                game_item['stop_time'] = stop_time
                game_item['game_no'] = game_no
                game_item['game_no_num'] = game_no_num
                game_item['bqc_bonus'] = bonus
                yield game_item



